<?php

class SearchController extends Controller {

    public $sort;
    public $order;
    public $page;
    public $pageNum;
    public $pageView;

//    public $layout = '//layouts/clearmain';

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow authenticated users to access all actions
                'users' => array('*'),
            ),
        );
    }

    public function init() {
        $this->sort = $_GET['sort'];
        $this->order = $_GET['order'];
        $this->page = !$_GET['page'] ? 1 : $_GET['page'];
        $this->pageNum = 16;
        $this->pageView = ($this->page - 1) * $this->pageNum;

        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/common.css');
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/v3/n_common.css');
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/common.js", CClientScript::POS_HEAD);
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/v3/n_common.js", CClientScript::POS_HEAD);
        $cs->registerCoreScript('jquery');
        $cs->registerCoreScript('jquery.ui');
    }

    public function actions() {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    public function actionIndex() {
        $keywords = urldecode($_GET['k']);
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/food.css');

        /*
         * 获取cookie商圈ID
         */
        $cookie = Yii::app()->request->getCookies();
        $zonecookie = (string) $cookie['zone'];
        $zonecookie = !$zonecookie ? '2' : $zonecookie;
        $zoneid = !$zonecookie ? '&fq=zoneId:2' : '&fq=zoneId:' . $zonecookie;

        if ($this->sort == '' && $this->order == '') {
            $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=*' . $keywords . '*' . $zoneid . '&fq=status:1&wt=json&indent=true&start=' . $this->pageView . '&rows=' . $this->pageNum);
        } else {
            $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=*' . $keywords . '*' . $zoneid . '&fq=status:1&wt=json&indent=true&start=' . $this->pageView . '&rows=' . $this->pageNum . '&sort=' . $this->sort . '+' . $this->order);
        }

        $model = CJSON::decode($row);

        /*
         * 分页
         */
        $numFound = $model['numFound'];
        $pager = new CPagination($numFound);
        $pager->pageSize = 16;

        $this->render('index', array('model' => $model['QueryResponse']['results'], 'numFound' => $numFound, 'pages' => $pager, 'keywords' => $keywords));
    }

    public function actionSearch() {
        $keywords = urlencode(trim($_GET['k']));
        $this->redirect(array('/search/index', 'k' => $keywords));
    }

}

?>